package com.jrx.anytxn.transaction.service.impl;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnRuntimeException;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.customer.entity.CmCustomerLabel;
import com.jrx.anytxn.customer.service.ICustomerLabelService;
import com.jrx.anytxn.param.dto.aviator.RuleMatchReq;
import com.jrx.anytxn.param.entity.PrPromotionInfo;
import com.jrx.anytxn.param.service.aviator.IAviatorService;
import com.jrx.anytxn.param.service.rights.IPrPromotionInfoService;
import com.jrx.anytxn.transaction.bean.PromotionLabelBean;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.service.IPromotionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 促销活动执行类
 */
@Service
public class PromotionServiceImpl implements IPromotionService {
    private static final Logger logger = LoggerFactory.getLogger(PromotionServiceImpl.class);

    @Autowired
    private IAviatorService aviatorService;
    @Autowired
    private IPrPromotionInfoService prPromotionInfoService;
    @Autowired
    private ICustomerLabelService customerLabelService;


    @Override
    public PromotionLabelBean matchingPromotion(String tenantId, String channel, String customerId, String productId, Date businessDate) {

        //获取客户标签
        CmCustomerLabel customerLabel = customerLabelService.matchingCustomerLabel(tenantId, channel, customerId, businessDate);

        if (customerLabel == null) {
            return null;
        }

        Map<String, Object> map = new HashMap<>();
        map.put(TransBizConstant.LOAN_RULE_TRANS_CHANNEL, channel);//渠道
        map.put(TransBizConstant.LOAN_RULE_LABEL_CODE, customerLabel.getLabelCode());//定价标签
        map.put(TransBizConstant.LOAN_RULE_PRODUCT_ID, productId);//产品Id

        RuleMatchReq ruleMatchReq = new RuleMatchReq(customerLabel.getOrganizationId(),
                null, TransBizConstant.PROMOTION_RULE_SET, map, tenantId);
        String promotionId = null;
        try {
            promotionId =
                    (String) aviatorService.executeRuleResultMap(ruleMatchReq).get(TransBizConstant.LOAN_RULE_RES_KEY);
        } catch (TxnBizException | TxnRuntimeException e) {
            logger.info("未匹配到促销活动");
        }
        PrPromotionInfo prPromotionInfo = null;
        if (StringUtils.isNotBlank(promotionId)) {
            prPromotionInfo = prPromotionInfoService.findByPromotionIdAndTenId(promotionId, tenantId);
        }
        if (prPromotionInfo == null) {
            return null;
        }
        //判断活动状态
        if (!TransBizConstant.STATUS_1.equals(prPromotionInfo.getTableStatus())) {
            //非正常状态  退出
            return null;
        }

        //判断活动有效期
        if (businessDate.compareTo(prPromotionInfo.getExpireDate()) > 0
                || businessDate.compareTo(prPromotionInfo.getEffectiveDate()) < 0) {
            //当前业务日期大于失效日期或小于生效日期  退出
            return null;
        }
        PromotionLabelBean promotionLabelBean = new PromotionLabelBean();
        promotionLabelBean.setCmCustomerLabel(customerLabel);
        promotionLabelBean.setPrPromotionInfo(prPromotionInfo);
        return promotionLabelBean;
    }
}
